// 帮助函数

glb = {
    
    // 把字符串变成竖起来的, 中间加入\n
    setUprightString : function(text) {
        l = text.split('');
        return l.join('\n'); 
    },
    
    // 简单的格式化数字到字符串的函数
    formatNumber : function(fmt, num) {
        //this.printCallStack();
        s = num.toString();
        // 裁剪
        if(s.length >= fmt.length) {
            s = s.substr(s.length - fmt.length, fmt.length);
            return s;
        }
        else {
            s = fmt.substr(0, fmt.length - s.length) + s;
            return s;
        }
    },

    // 格式化秒数 
    formatTime : function(scd) {
        hh = Math.floor(scd / 3600);
        mm = Math.floor(scd / 60) % 60;
        ss = scd % 60;
        return '' + hh + ':' + this.formatNumber('00', mm) + ':' + this.formatNumber('00', ss);
    },

    // 打印堆栈, 会有很多内容
    printCallStack : function() {
        var i = 0;  
        var fun = arguments.callee;  
        do {  
            fun = fun.arguments.callee.caller;  
            cc.log(++i + ': ' + fun);  
        } while (fun);  
    },

    // 打印Object
    MAX_DUMP_DEPTH : 10,
    dumpObject : function (obj, name, indent, depth) {
        if (depth > this.MAX_DUMP_DEPTH) {  
            return indent + name + ": <Maximum Depth Reached>\n";  
        }
        if (typeof obj == "object") {  
            var child = null;  
            var output = indent + name + "\n";  
            indent += "\t";  
            for (var item in obj) {  
                 try {  
                    child = obj[item];  
                } catch (e) {  
                    child = "<Unable to Evaluate>";  
                }  
                if (typeof child == "object") {
                    output += this.dumpObject(child, item, indent, depth + 1);  
                } else {  
                    output += indent + item + ": " + child + "\n";  
                }  
            }  
            return output;  
        } else {  
            return obj;  
        }  
    },

    printObject : function (obj) {
        cc.log(this.dumpObject(obj, 'ROOT', 0, 0));
    },
    
    // int转成cc.c4b
    int2color : function (n) {
        var a = 0xff;
        // 自带alpha
        if (n > 0xffffff) { a = parseInt(n / 0x1000000); n = n - a * 0x1000000; }
        var r = parseInt(n / 0x10000); n = n - r * 0x10000;
        var g = parseInt(n / 0x100); n = n - g * 0x100;
        var b = n;
        return cc.c4b(r,g,b,a);
    },

    // 显示图片百分比 参数:0~1 (比如:经验槽) (isRotated:生成png整合图的时候是否旋转了)
    displayTexturePercent : function (uiImage, p, isRotated) {
        if (isRotated == null) isRotated = false;
        else isRotated = true;

        p = Math.min( Math.max(p, 0), 1 );
        var s = uiImage.getContentSize();   // 原始大小
        var r = uiImage.getTextureRect();
        /*
        cc.log('p:'+p);
        cc.log('getTextureRect:  ------------- 1:')
        cc.dump(r);
        cc.log('getContentSize:  -------------')
        cc.dump(s);
        */
        r.width = s.width * p; 
        /*
        cc.log('getTextureRect   ------------- 2:')
        cc.dump(r);
        */
        // setTextureRect(rect, isRotated, rect.size)
        uiImage.setTextureRect(r, !isRotated, cc.size(r.width, r.height));

        //uiImage.setTextureRect(r);
        uiImage.setContentSize(s);  // 设置回原始的大小, 不然下次取出的大小就是裁剪后的大小
    },

    // 数组求和, 兼容字符串
    sum : function (list, isString) {
        var res = eval(list.join("+"));
        if (res == null) {
            if (isString) res = '';
            else res = 0;
        }
        return res;
    },

    // 获取时间戳
    timestamp : function () {
        return new Date().getTime();
    },

};
